<?php
App::uses('AppModel', 'Model');
App::uses('CompositeKeyModel', 'Model');

/* 
 * @property Member $Member
 * @property Organisation $Organisation
 */
class MemberImage extends CompositeKeyModel {
    
    public $useDbConfig = "organisations";
    
    public $belongsTo = array(
        'Member' => array(
            'className' => 'Member',
            'foreignKey' => 'member_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
    
    public $hasMany = array(
        'OrganisationMember' => array(
            'className' => 'OrganisationMember',
            'foreignKey' => 'member_image_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    public $hasOne = array(
        'OrganisationMemberImage' => array(
            'className' => 'OrganisationMemberImage',
            'foreignKey' => 'member_image_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );
    
    public function __construct($id = false, $table = null, $ds = null) {
        
        $save_dir_root = Configure::read('file_upload_root_directory');
        $host_server = Configure::read('file_upload_host_server');
    
        $this->virtualFields = array(
            'url' => "CONCAT(\"$host_server\", \"$save_dir_root\", \"/\", MemberImage.file_path, \"/\", MemberImage.file_name)",
            'thumb_url' => "CONCAT(\"$host_server\", \"$save_dir_root\", \"/\", MemberImage.thumb_path, \"/\", MemberImage.file_name)"
        );
        
        parent::__construct($id, $table, $ds);
    }
    
    public function getUntagged($org_id, $offset = 0, $limit = 20) {
        return $this->find('all', array(
            'conditions' => array(
                'MemberImage.member_id' => null
            ),
            'limit' => $limit,
            'offset' => $offset,
            'recursive' => -1
        ));
    }
    
    /**
     * Unassigns any images assigned to the specified member_id
     * 
     * @param type $member_id
     */
    public function unassign($organisation_id, $member_id) {
        $this->OrganisationMember->updateAll(array('member_image_id' => null), array('organisation_id' => $organisation_id, 'member_id' => $member_id));
    }
    
    /**
     * Assigns a specific member image to a member
     * 
     * @param type $member_image_id
     * @param type $member_id
     */
    public function assign($member_image_id, $organisation_id, $member_id) {
        $OMI = ClassRegistry::init('OrganisationMemberImage');
        
        $member = $this->OrganisationMember->find('first', array(
            'contain' => array('OrganisationMemberImage'),
            'conditions' => array(
                'organisation_id' => $organisation_id,
                'member_id' => $member_id
            )
        ));
        
        if( isset($member['OrganisationMemberImage']['id']) ) {
            return $OMI->updateAll(array('member_image_id' => $member_image_id), array('id' => $member['OrganisationMemberImage']['id']));
        }
        
        else {
            return $OMI->save(array(
                'organisation_id' => $organisation_id,
                'organisation_member_id' => $member['OrganisationMember']['id'],
                'member_image_id' => $member_image_id
            ));
        }
    }
}